package net.j4love.json.jackson.xml;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author he peng
 * @create 2018/1/8 15:26
 * @see
 */
public class JacksonXmlTest {

    @Test
    public void deserialize() throws Exception {
        InputStream xmlStream = JacksonXmlTest.class.getClassLoader().getResourceAsStream("pay-response.xml");
        XmlMapper xmlMapper = new XmlMapper();
        PayPlatResponseParameter payPlatResponseParameter = xmlMapper.readValue(xmlStream, PayPlatResponseParameter.class);
        System.out.println(payPlatResponseParameter);
    }

    @Test
    public void serialize() throws Exception {
        XmlMapper xmlMapper = new XmlMapper();
        PayPlatResponseHeader responseHeader = new PayPlatResponseHeader();
        responseHeader.setBizNo(1243324324L)
                    .setErrorCode("12333")
                    .setErrorMsg("dsae984732984")
                    .setSignvalue("dsadjhaskj389473298yhfkj")
                    .setSuccessAmount(1)
                    .setSuccessCount(1)
                    .setTotalCount(1)
                    .setTotalFee(2);

        PayPlatPayItem payPlatPayItem = new PayPlatPayItem();
        payPlatPayItem.setAmount(1)
                    .setErrorCode("dsa4324")
                    .setErrorMsg("dsadkasjhi324")
                    .setHnapaySeqNo(23214325325L)
                    .setOrderId("dsadjha34324113")
                    .setStatus(1)
                    .setSuccessTime(System.currentTimeMillis());
        List<PayPlatPayItem> list = new ArrayList<>();
        list.add(payPlatPayItem);
        PayPlatResponseBody responseBody = new PayPlatResponseBody();
        responseBody.setPayItem(list);
        PayPlatResponseParameter payPlatResponseParameter = new PayPlatResponseParameter();
        payPlatResponseParameter.setResponseHeader(responseHeader)
                                .setResponseBody(responseBody);

        String xml = xmlMapper.writeValueAsString(payPlatResponseParameter);
        System.out.println(xml);
    }
}
